/*
*** (C) 2008-2009 UCMai.com ***

Subj: 组管理
Auth: zhouyongtai
Date: 2009-02-20
*/

c.pgsubject = '组';

var acliid = 0;
var aclfid = '';

var GroupCoreCore = function(){
	//修改记录时表单需要绑定的当前值字段
	this.formReader = [
		{name:'Subject'},
		{name:'Remark'},
		{name:'ReportDay'},
		{name:'DepartmentID'},
		{name:'DepartName'}
	];
	var departmentStore = new Ext.data.Store({
		autoLoad:true,
		proxy:new Ext.data.HttpProxy({url:_S_Department_NameList}),
		reader:new Ext.data.JsonReader({
			fields:[{name:'DepartmentID',type:'int'},
				{name:'DepartmentName'}],
			root:'records'
		})
	});
	//表单项
	this.formItems = [
		{xtype:'hidden',name:'a',id:'se_o_cmdid',value:'0'},
		{xtype:'hidden',name:'li',id:'se_o_li',value:'0'},
		{xtype:'hidden',name:'ln',id:'se_o_ln',value:''},
		{xtype:'hidden',name:'GroupID',id:'GroupID',value:'0'},
		{
			xtype:'combo',
			store:departmentStore,
			displayField:'DepartmentName',
			valueField:'DepartmentID',
			fieldLabel:'部门',
			name:'DepartmentID',
			hiddenName:'DepartmentID',
			id:'se_o_departmentid',
			mode:'local',
			triggerAction: 'all',
			blankText:'请选择部门',
			emptyText:'选择部门',
			allowBlank:false,
			anchor:'85%',
			editable: false
		},{
			fieldLabel: '组名称',
			name: 'Subject',
			id: 'se_o_subject',
			anchor: '85%',
			maxLength: '32',
			allowBlank: false
		},
		{
			fieldLabel: '查看天数',
			name: 'ReportDay',
			id: 'se_o_ReportDay',
			anchor: '85%',
			maxLength: '32'
		},
		{
			xtype:'textarea',
			fieldLabel: '备注',
			name: 'Remark',
			id:'se_o_remark',
			width:180,
			height:80
		}
	];

	//查看项字段
	this.viewReader = [
		{name:'Subject'},
		{name:'Remark'},
		{name:'ListIndex'},
		{name:'OptionSet'},
		{name:'LockStatus'},
		{name:'ReportDay'},
		{name:'DepartmentID'},
		{name:'DepartName'}
	];

	//Grid列头
	this.gridHeader = [
		{header:'组名称',dataIndex:'Subject'},
		{header:'用户数',dataIndex:'UsersNumber'},
		{header:'部门',dataIndex:'DepartName'},
		{header:'报表查看天数',dataIndex:'ReportDay'},
		{header:'备注',dataIndex:'Remark'},
		{header:'状态',dataIndex:'LockStatus',renderer:function(value){
			return '<b><font color="'+ c.aLock[value][1] +'">'+ c.aLock[value][0] +'</font></b>'
		}}
	];

	//Grid绑定的字段
	this.gridStore = [
		{name:'GroupID'},
		{name:'Subject'},
		{name:'UsersNumber'},
		{name:'Remark'},
		{name:'LockStatus'},
		{name:'ReportDay'},
		{name:'DepartmentID'},
		{name:'DepartName'}
	];

	//显示系统用户
	this.showUser = function(){
		//permit();
		var GroupID = this.getSelectionModel().getSelected().data.GroupID;
		var title = '查看系统用户：' + this.getSelectionModel().getSelected().data.Subject;

		acliid ++;
		aclfid = 'sysuser' + acliid.toString();

		this.oMainPanel = new Ext.Panel({
			border:false,
			layout:'fit',
			html:'<iframe src="../../s/SysUserList.asp?GroupID=' + GroupID + '" marginheight="0" marginwidth="0" scrolling="auto" frameborder="0" name="' + aclfid + '" id ="se_o_' + aclfid + '" width="100%" height=425></iframe>'
		});

		this.window = new Ext.Window({
			xtype:'window',
			title: title,
			buttonAlign : 'right',
			closable:true,
			resizable: false,
			modal: 'true',
			layout:'fit',
			width:800,
			height:500,
			closeAction:'hide',
			plain: true,
			items:[this.oMainPanel],
			buttons: [{
				text: c.bcancel,
				handler: function(){
					this.window.hide();
				}.createDelegate(this)
			}]

		});
		this.window.on('hide',function(){
			/* 删除动态创建的iframe对象 */
			var ifr = window.top.document.getElementById('se_o_' + aclfid);
			ifr.parentNode.removeChild(ifr);
		}.createDelegate(this)
		);
		this.window.show();
	}


//-----------设置游戏
	//显示组游戏
	this.showGame = function(){
		//permit();
		var GroupID = this.getSelectionModel().getSelected().data.GroupID;
		var title = '设置游戏:' + this.getSelectionModel().getSelected().data.Subject;

		acliid ++;
		aclfid = 'sysgame' + acliid.toString();

		this.oMainPanel = new Ext.Panel({
			border:false,
			layout:'fit',
			html:'<iframe src="../../s/GroupGame.asp?li=' + GroupID + "&ajaxcachebust=" + new Date().getTime() +'" marginheight="0" marginwidth="0" scrolling="auto" frameborder="0" name="' + aclfid + '" id ="se_o_' + aclfid + '" width="100%" height=425></iframe>'
		});

		this.window = new Ext.Window({
			xtype:'window',
			title: title,
			buttonAlign : 'right',
			closable:true,
			resizable: false,
			modal: 'true',
			layout:'fit',
			width:800,
			height:500,
			closeAction:'hide',
			plain: true,
			items:[this.oMainPanel],
			buttons: [{
				id: 'btnSave',
				text: c.bsave,
				handler: SetGame.createDelegate(this,[GroupID]),
				disabled: false
			},{
				text: c.bcancel,
				handler: function(){
					this.window.hide();
				}.createDelegate(this)
			},{
				id: 'btnReset',
				text: c.breset,
				handler: ResetGame,
				disabled: false
			}]

		});
		this.window.on('hide',function(){
			/* 删除动态创建的iframe对象 */
			var ifr = window.top.document.getElementById('se_o_' + aclfid);
			ifr.parentNode.removeChild(ifr);
		}.createDelegate(this)
		);
		this.window.show();
	}

	//保存设置
	var SetGame = function(groupid){
		var selectedIds = window.top.frames[aclfid].getSelectedID();
		zyt.ajax(_C_GroupCore_X, {a:12,aid:selectedIds,li:groupid}, function(){
			Ext.HandleTip.msg(c.opres,c.selitem + c.save + c.ok);
			this.window.hide();
		}.createDelegate(this));
	};

	//重置
	var ResetGame = function(){
		window.top.frames[aclfid].document.getElementById('se_o_frmGame').reset();
	};

//----------游戏设置完成


	//显示组站点
	this.showsite = function(){
		//permit();
		var GroupID = this.getSelectionModel().getSelected().data.GroupID;
		var title = '设置站点:' + this.getSelectionModel().getSelected().data.Subject;

		acliid ++;
		aclfid = 'syssite' + acliid.toString();

		this.oMainPanel = new Ext.Panel({
			border:false,
			layout:'fit',
			html:'<iframe src="../../s/Groupsite.asp?li=' + GroupID + "&ajaxcachebust=" + new Date().getTime() +'" marginheight="0" marginwidth="0" scrolling="auto" frameborder="0" name="' + aclfid + '" id ="se_o_' + aclfid + '" width="100%" height=425></iframe>'
		});

		this.window = new Ext.Window({
			xtype:'window',
			title: title,
			buttonAlign : 'right',
			closable:true,
			resizable: false,
			modal: 'true',
			layout:'fit',
			width:800,
			height:500,
			closeAction:'hide',
			plain: true,
			items:[this.oMainPanel],
			buttons: [{
				id: 'btnSave',
				text: c.bsave,
				handler: Setsite.createDelegate(this,[GroupID]),
				disabled: false
			},{
				text: c.bcancel,
				handler: function(){
					this.window.hide();
				}.createDelegate(this)
			},{
				id: 'btnReset',
				text: c.breset,
				handler: Resetsite,
				disabled: false
			}]

		});
		this.window.on('hide',function(){
			/* 删除动态创建的iframe对象 */
			var ifr = window.top.document.getElementById('se_o_' + aclfid);
			ifr.parentNode.removeChild(ifr);
		}.createDelegate(this)
		);
		this.window.show();
	}

	//保存设置
	var Setsite = function(groupid){
		var selectedIds = window.top.frames[aclfid].getSelectedID();
		zyt.ajax(_C_GroupCore_X, {a:13,aid:selectedIds,li:groupid}, function(){
			Ext.HandleTip.msg(c.opres,c.selitem + c.save + c.ok);
			this.window.hide();
		}.createDelegate(this));
	};

	//重置
	var Resetsite = function(){
		window.top.frames[aclfid].document.getElementById('se_o_frmsite').reset();
	};

	//权限设定
	this.showSetAcl = function(){
		//permit();
		var GroupID = this.getSelectionModel().getSelected().data.GroupID;
		var title = '设置权限【组】：' + this.getSelectionModel().getSelected().data.Subject;

		acliid ++;
		aclfid = 'myframe' + acliid.toString();

		this.oMainPanel = new Ext.Panel({
			border:false,
			layout:'fit',
			html:'<iframe src="../../s/setobjectacl.asp?GroupID=' + GroupID + "&ajaxcachebust=" + new Date().getTime() + '" marginheight="0" marginwidth="0" scrolling="auto" frameborder="0" name="' + aclfid + '" id ="se_o_' + aclfid + '" width="100%" height=465></iframe>'
		});

		this.window = new Ext.Window({
			xtype:'window',
			title: title,
			buttonAlign : 'right',
			closable:true,
			resizable: false,
			modal: 'true',
			layout:'fit',
			width:800,
			height:500,
			closeAction:'hide',
			plain: true,
			items:[this.oMainPanel],
			buttons: [{
				id: 'btnSave',
				text: c.bsave,
				handler: SetAcl.createDelegate(this,[GroupID]),
				disabled: false
			},{
				text: c.bcancel,
				handler: function(){
					this.window.hide();
				}.createDelegate(this)
			},{
				id: 'btnReset',
				text: c.breset,
				handler: ResetAcl,
				disabled: false
			}]

		});
		this.window.on('hide',function(){
			/* 删除动态创建的iframe对象 */
			var ifr = window.top.document.getElementById('se_o_' + aclfid);
			ifr.parentNode.removeChild(ifr);
		}.createDelegate(this)
		);
		this.window.show();
	};

	//保存设置
	var SetAcl = function(groupid){
		var selectedIds = window.top.frames[aclfid].getSelectedID();
		zyt.ajax(_C_GroupCore_X, {a:11,aid:selectedIds,li:groupid}, function(){
			Ext.HandleTip.msg(c.opres,c.selitem + c.save + c.ok);
			this.window.hide();
		}.createDelegate(this));
	};

	//重置
	var ResetAcl = function(){
		window.top.frames[aclfid].document.getElementById('se_o_frmacl').reset();
	};

	//编辑
	this.showWindow = function(str){
		//permit();
		this.form = new Ext.FormPanel({
				labelAlign:'left',
				bodyStyle:'padding:5px',
				frame:true,
				labelWidth:60,
				reader : new Ext.data.JsonReader({
					root : 'record',
					success: '@success'
				},this.formReader),
				items:[
					{
						layout:'form',
						defaultType:'textfield',
						border:false,
						labelSeparator:'：',
						items:this.formItems
				}]
		});
		this.form.getForm().reset();
		this.window = new Ext.Window({
			xtype:'window',
			buttonAlign : 'right',
			closable:true,
			resizable: false,
			layout:'fit',
			modal: 'true',
			width:320,
			height:220,
			closeAction:'hide',
			plain: true,
			items:[this.form],
			keys: {
				key: Ext.EventObject.ENTER,
				fn: save.createDelegate(this,[str])
			},
			buttons: [{
				id: 'btnSave',
				text: c.bsave,
				handler: save.createDelegate(this,[str]),
				disabled: false
			},{
				text: c.bcancel,
				handler: function(){
					this.window.hide();
				}.createDelegate(this)
			}]
		});
		this.window.on('hide',function(){
			this.form.destroy();
		}.createDelegate(this));

		//this.window.show(this.getEl());
		this.window.show();
		this.form.findById('se_o_subject').focus(false,true);
		this.window.setTitle((str)?c.ad():c.md() + " #" + this.getSelectionModel().getSelected().data.GroupID);
		if(!str){
			/* local mode */
			this.form.getForm().setValues(this.getSelectionModel().getSelected().data);
		}
	};

	//保存
	var save = function(str){
		if (!this.form.getForm().isValid()){
			return;
		}

		var selectedId = this.getSelectedIds();
		if(str){selectedId = 0;}

		if(selectedId>0){
			this.form.findById('se_o_cmdid').setValue(6);
			this.form.findById('GroupID').setValue(selectedId);
		}else{
			this.form.findById('se_o_cmdid').setValue(5);
		}

		showLoading(c.g(c.e.save),c.loading);

		zyt.ajax(_C_GroupCore_S,this.form.getForm().getValues(), function(){
			hideLoading();
			this.window.hide();
			var act = selectedId>0?c.modify:c.add;
			Ext.HandleTip.msg(c.opres,c.item +  act +c.ok);
			this.reload();
		}.createDelegate(this));
	};

	//删除
	var remove = function(){
		//permit();
		var delids = this.getSelectedIds();
		var title = "组" + (Ext.isNumber(delids) ? "（" + this.getSelectionModel().getSelected().data.Subject + "）" : "");
		zyt.confirmEx(c.del, title, function(){
			zyt.ajax(_C_GroupCore_X_Del, {a:4,aid:delids}, function(){
				Ext.HandleTip.msg(c.opres,c.selitem + c.del + c.ok);
				this.reload();
			}.createDelegate(this));
		}.createDelegate(this));
	};

	var gridReader = new Ext.data.JsonReader({
		totalProperty:'totalProperty',
		root:'records'
		},this.gridStore);

	this.cm = new Ext.grid.ColumnModel(this.gridHeader);
	this.cm.defaultSortable = true;
	this.store = new Ext.data.Store({
		proxy:new Ext.data.HttpProxy({url:_C_GroupCore_X}),
		reader:gridReader,
		baseParams: {limit:c.page.LargePageSize}
	});

	//锁定
	var lock = function(str){
		//permit();
		var act=str?c.lock:c.unlock;
		Ext.MessageBox.confirm(c.info,'您确定要将所选用户'+act,function(btn){
			if(btn=='yes'){
				var delids = this.getSelectedIds();
				zyt.ajax(_C_GroupCore_X_Lock, {a:2,aid:delids}, function(){
					Ext.HandleTip.msg('操作结果',c.selitem + act + c.ok);
					this.reload();
				}.createDelegate(this));
			}
		}.createDelegate(this));
	};

	//生成系统架构
	var createprofile = function(){
		//permit();
		var selectedId = this.getSelectedIds();
		var ids = '',length= 0,msg = '';
		confirmEx('生成','用户系统架构',function(){
			if(typeof selectedId == 'number') {
				zyt.ajax(_C_GroupUser_Create, {groupid:selectedId}, function(result){
					result = (result=='')?'系统架构生成成功':'生成用户[' + result.toString() + ']的系统架构时发生错误！';
					Ext.HandleTip.msg('操作结果',result);
				}.createDelegate(this));
			}else{
				ids = selectedId.split(',');
				var len = ids.length;
				for(var i=0;i<len;i++){
					zyt.ajax(_C_User_Create, {createuserid:ids[i]}, function(result){
						msg += (result=='')?'':'生成用户[' + result.toString() + ']的系统架构时发生错误！\n';
					}.createDelegate(this));
				}
				msg = (msg=='')?'系统架构生成成功，共生成' + len.toString() + '个！':msg;
				Ext.HandleTip.msg('操作结果',msg);
			}
		}.createDelegate(this));
	};

	GroupCoreCore.superclass.constructor.call(this, {
		region: 'center',
		store: this.store,
	
		keyField: 'GroupID',
		viewConfig: {forceFit:true},
		tbar:[{
				id:'btnAdd',
				text:c.add,
				iconCls:'add',
				handler: this.showWindow.createDelegate(this, [true])
			}, '-',
			{
				id:'btnModify',
				text:c.modify,
				iconCls:'modify',
				disabled:true,
				handler:this.showWindow.createDelegate(this, [false])
			},'-',{
				id:'btnDel',
				text:c.del,
				iconCls:'remove',
				disabled:true,
				handler:remove.createDelegate(this)
			},'-',{
				id:'btnLock',
				text:c.lock,
				iconCls:'lock',
				disabled:true,
				handler:lock.createDelegate(this)
			},'-',{
				id:'btnPower',
				text:'设置权限',
				iconCls:'user',
				disabled:true,
				handler:this.showSetAcl.createDelegate(this)
			},'-',{
				id:'btnView',
				text:'查看用户',
				iconCls:'user',
				disabled:true,
				handler:this.showUser.createDelegate(this)
			},'-',{
				id:'btnGame',
				text:'设置游戏',
				iconCls:'user',
				disabled:true,
				handler:this.showGame.createDelegate(this)
			},'-',{
				id:'btnSite',
				text:'设置站点',
				iconCls:'user',
				disabled:true,
				handler:this.showsite.createDelegate(this)
			},'-',{
				id:'btnCreate',
				text:'生成框架',
				iconCls:'user',
				disabled:true,
				handler:createprofile.createDelegate(this, [false])
		}],
		page: true
	});

	this.getSelectionModel().on('selectionchange', function(){
		var haveselected = this.getSelectedIds();
		if(haveselected!='0'){
			if(typeof haveselected==='number'){
				Ext.getCmp('btnModify').setDisabled(false);
				Ext.getCmp('btnPower').setDisabled(false);
				Ext.getCmp('btnView').setDisabled(false);
				Ext.getCmp('btnGame').setDisabled(false);
				Ext.getCmp('btnSite').setDisabled(false);
				Ext.getCmp('btnCreate').setDisabled(false);
			}else{
				Ext.getCmp('btnModify').setDisabled(true);
				Ext.getCmp('btnPower').setDisabled(true);
				Ext.getCmp('btnView').setDisabled(true);
				Ext.getCmp('btnGame').setDisabled(true);
				Ext.getCmp('btnSite').setDisabled(true);
				Ext.getCmp('btnCreate').setDisabled(true);
			}
			Ext.getCmp('btnDel').setDisabled(false);
			Ext.getCmp('btnLock').setDisabled(false);
		}else{
			Ext.getCmp('btnModify').setDisabled(true);
			Ext.getCmp('btnPower').setDisabled(true);
			Ext.getCmp('btnDel').setDisabled(true);
			Ext.getCmp('btnLock').setDisabled(true);
			Ext.getCmp('btnView').setDisabled(true);
			Ext.getCmp('btnGame').setDisabled(true);
			Ext.getCmp('btnSite').setDisabled(true);
			Ext.getCmp('btnCreate').setDisabled(true);
		}
	}.createDelegate(this));

	this.on("rowdblclick", function(grid,rowIndex){
		this.showUser();
	});

	var onContextMenu = function(grid,rowIndex,e){
		e.stopEvent();
		grid.getSelectionModel().selectRow(rowIndex);
		var messageContextMenu = new Ext.menu.Menu({
			id: 'messageContextMenu',
			items : [{
				id:'rAdd',
				text : c.add,
				iconCls:'add',
				handler: this.showWindow.createDelegate(this, [true])
			},{
				id:'rModify',
				text : c.modify,
				iconCls:'modify',
				handler:this.showWindow.createDelegate(this, [false])
			},{
				id:'rDel',
				text : c.del,
				iconCls:'remove',
				handler:remove.createDelegate(this)
			},{
			id:'rLock',
			text:c.lock,
			iconCls:'lock',
			handler:lock.createDelegate(this)
			},'-',{
			id:'rPower',
			text:'设置权限',
			iconCls:'user',
			handler:this.showSetAcl.createDelegate(this)
			},'-',{
			id:'rgame',
			text:'设置组游戏',
			iconCls:'user',
			handler:this.showGame.createDelegate(this)
			},'-',{
			id:'rSite',
			text:'设置组站点',
			iconCls:'user',
			handler:this.showsite.createDelegate(this)
			}]
		});
		messageContextMenu.showAt(e.getXY());
		e.preventDefault();
	};
	this.on('rowcontextmenu',onContextMenu);

	this.reload = function(){
		this.store.removeAll();
		this.store.reload({params:{start:0, limit:c.page.LargePageSize}});
	};

	this.on("activate", function(grid){
		detectAcl(_C_GroupCore_X);
		this.store.load({params:{start:0, limit:c.page.LargePageSize}});
	});
};
Ext.extend(GroupCoreCore,Ext.ux.grid.GridPanel);